#include "MeasureTool.h"
#include "MapCanvas.h"
#include "RubberBand.h"
#include "MeasureDialog.h"
#include "Cursors.h"

#include <TGS/MapControl2.0/MapToPixel.h>
#include <QMessageBox>
#include <QMouseEvent>
#include <QSettings>

namespace TGS{
namespace MapControl{

MeasureTool::MeasureTool(MapCanvas* canvas, bool measureArea )
    : MapTool( canvas )
{
	_measureArea = measureArea;

	_rubberBand = new RubberBand(canvas, _measureArea);

	QPixmap myCrossHairQPixmap = QPixmap(( const char ** ) cross_hair_cursor );
	setCursor(QCursor(myCrossHairQPixmap, 8, 8 ));

	_done = true;
	// Append point we will move
	_points.append(Point( 0, 0 ) );

	_measureDialog = new MeasureDialog( this, Qt::WindowStaysOnTopHint );
}

MeasureTool::~MeasureTool()
{
	
}


const QList<TGS::GeoMath::Point>& MeasureTool::points()
{
  return _points;
}

void MeasureTool::activate()
{
	_measureDialog->restorePosition();
	MapTool::activate();

	// ensure that we have correct settings
	updateSettings();
}

void MeasureTool::deactivate()
{
	_measureDialog->close();
	_rubberBand->reset();
	MapTool::deactivate();
}


void MeasureTool::restart()
{
	_points.clear();

	_rubberBand->reset(_measureArea);

	// re-read settings
	updateSettings();

	_done = true;
}

void MeasureTool::updateSettings()
{
 
}

//////////////////////////

void MeasureTool::canvasPressEvent( QMouseEvent * e )
{
	if ( e->button() == Qt::LeftButton )
	{
		if ( _done )
		{
			_measureDialog->restart();
			Point point = snapPoint( e->pos() );
			addPoint( point );
			_done = false;
		}
	}
}

void MeasureTool::canvasMoveEvent( QMouseEvent * e )
{
	if ( ! _done )
	{
		Point point = snapPoint( e->pos() );

		_rubberBand->movePoint( point );
		if ( ! _points.isEmpty() )
		{
			// Update last point
			_points.removeLast();
			_points.append( point ) ;
			_measureDialog->mouseMove( point );
		}
	}
}


void MeasureTool::canvasReleaseEvent( QMouseEvent * e )
{
	Point point = snapPoint( e->pos() );
	if ( e->button() == Qt::LeftButton )
	{
		// Append point we will move
		addPoint( point );
		
		if(!_measureDialog->isVisible())
		{
			_measureDialog->setGeometry(e->globalX() + 30, e->globalY() + 30, 
				_measureDialog->width(), _measureDialog->height());
		}
		
		_measureDialog->show();
	}

}


void MeasureTool::addPoint( const Point &point )
{
	int last = _points.size() - 1;
	// don't add points with the same coordinates
	if ( _points.size() > 1 && _points[ last ] == _points[ last - 1 ] )
	return;

	Point pnt( point );
	// Append point that we will be moving.
	_points.append( pnt );

	_rubberBand->addPoint( point );
	if ( ! _done )
	{
		_measureDialog->addPoint( point );
	}
}

TGS::GeoMath::Point MeasureTool::snapPoint( const QPoint& p )
{
	return canvas()->getCoordinateTransform()->screenToMap( p );
}

void MeasureTool::canvasDoubleClickEvent( QMouseEvent * e )
{
	if ( e->button() == Qt::LeftButton )
	{
		if (_done)
		{
			_measureDialog->restart();
		}
		else
		{
			// The figure is finished
			_done = true;
			_measureDialog->show();
		}
	}

	
}




}//MapControl
}//TGS
